// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import { Publish } from "../../meshTools/tools/Publish";
import { RoomUser, UserInfo } from "../bean/GameBean";
import { GlobalBean } from "../bean/GlobalBean";
import { EventType } from "../common/EventCenter";
import { GameMgr } from "../common/GameMgr";
import { AutoMessageBean, AutoMessageId } from "../net/MessageBaseBean";
import MatchItemController from "../pfb/MatchItemController";
import { Config } from "../util/Config";
import { Tools } from "../util/Tools";

const { ccclass, property } = cc._decorator;

@ccclass
export default class MatchParentController extends cc.Component {

    @property(cc.Node)
    backBtn: cc.Node = null;
    @property(cc.Node)
    matchUserLay: cc.Node = null;
    @property(cc.Node)
    matchUserLay2: cc.Node = null;
    @property(cc.Prefab)
    matchItem: cc.Prefab = null;

    backClick: Function

    private _userListCol: MatchItemController[] = []; //显示用户布局的controller

    normalTime = () => { }

    protected onLoad(): void {
        if (cc.sys.os === cc.sys.OS_IOS) {
            // 在iOS设备上执行的代码
            if (Publish.GetInstance().gameMode != '2') {//非半屏的话
                this.backBtn.getComponent(cc.Widget).top = 146
            }
        } else {
            // 在其他设备上执行的代码
        }
    }

    protected onEnable(): void {
        this.backBtn.active = true
    }

    start() {

        //设置返回键的点击事件
        Tools.imageButtonClick(this.backBtn, Config.buttonRes + 'board_btn_back_normal', Config.buttonRes + 'board_btn_back_pressed', () => {
            if (this.backClick) {
                this.backClick()
            }
        });

    }

    createMatchView() {

        let user_s: RoomUser[] = [];

        //判断是几人游戏
        let peopleNumber: number = GlobalBean.GetInstance().players;
        let userInfo: UserInfo = GlobalBean.GetInstance().loginData.userInfo;
        for (let i = 0; i < peopleNumber; i++) {
            if (i == 0) {

                let user: RoomUser = {
                    userId: userInfo.userId,
                    nickName: userInfo.nickname,
                    avatar: userInfo.avatar,
                    pos: 0,
                    coin: 0,
                    status: 0,
                    score: 0,
                    rank: 0,
                    playerNumber: 0,
                    x: 0,
                    y: 0,
                    playerType: "",
                    surviveTime: 0
                }
                user_s.push(user);

            } else {
                user_s.push(null);
            }
        }
        //这里是添加一个等待的页面匹配的占位
        this.matchUserLay.removeAllChildren();
        this.matchUserLay2.removeAllChildren();
        this._userListCol = [];

        if (user_s.length <= 4) {
            for (let i = 0; i < user_s.length; i++) {
                const item = cc.instantiate(this.matchItem);
                this.matchUserLay.addChild(item);
                let matchingItemController = item.getComponent(MatchItemController)
                this._userListCol.push(matchingItemController)
                matchingItemController.setData(user_s[i]);
            }
        } else {
            let arrayData = Tools.chunkArray(user_s, 3)//根据人数进行分块
            for (let i = 0; i < arrayData[0].length; i++) {
                const item = cc.instantiate(this.matchItem);
                this.matchUserLay.addChild(item);
                let matchingItemController = item.getComponent(MatchItemController)
                this._userListCol.push(matchingItemController)
                matchingItemController.setData(user_s[i]);
            }
            for (let i = 0; i < arrayData[1].length; i++) {
                const item = cc.instantiate(this.matchItem);
                this.matchUserLay2.addChild(item);
                let matchingItemController = item.getComponent(MatchItemController)
                this._userListCol.push(matchingItemController)
                matchingItemController.setData(user_s[3+i]);
            }
        }



    }

    //设置游戏数据
    setGameData() {
        this.backBtn.active = false //匹配成功之后 隐藏掉返回键
        let user: RoomUser[] = GlobalBean.GetInstance().adjustUserData();
        const index = user.findIndex((item) => item.userId === GlobalBean.GetInstance().loginData.userInfo.userId);//搜索
        GlobalBean.GetInstance().loginData.userInfo.coin = user[index].coin //更新自己的最新金币
        //这里是匹配成功之后的真实数据
        for (let i = 0; i < user.length; i++) {
            if (i <= this._userListCol.length) {
                this._userListCol[i].setData(user[i]);
            }
        }

        this.normalTime = () => {
            let autoMessageBean: AutoMessageBean = {
                'msgId': AutoMessageId.SwitchGameSceneMsg,//匹配成功 通知进入游戏页面
                'data': {}
            }
            GameMgr.Event.Send(EventType.AutoMessage, autoMessageBean);//进入游戏的消息
        }

        this.scheduleOnce(this.normalTime, 1)

    }


    protected onDisable(): void {
        this.unschedule(this.normalTime)
    }




    setClick(backClick: Function) {
        this.backClick = backClick
    }

    // update (dt) {}

    
}
